import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.JButton;
import javax.swing.border.LineBorder;
import javax.swing.JColorChooser;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 * GEdgeDialog is the dialog for modifying various properties of an edge.
 *
 * @version 1.0
 * @author P.Jaya Krishna.
 */
class GEdgeDialog extends JDialog implements ActionListener
{
	private JLabel colorLabel;
	private JSpinner weight;
	
	GPanel gPanel;
	EdgeAttr currentEdge;
	Color edgeColor;
	
	/**
	 * Creates a dialog for the specified edge "currentEdge" of a GPanel "gPanel".
	 */
	public GEdgeDialog(GPanel gPanel, EdgeAttr currentEdge)
	{
		super(gPanel.gFrame, "Edge Properties", true);
		this.gPanel = gPanel;
		this.currentEdge = currentEdge;
		edgeColor = currentEdge.getColor();
		
		JButton ok    = new JButton("Ok");
		JButton cancel= new JButton("Cancel");
		JPanel bpanel = new JPanel();
		bpanel.add(ok);
		bpanel.add(cancel);
		ok.addActionListener(this);
		cancel.addActionListener(this);
		
		JPanel cpanel	= new JPanel();
		cpanel.setLayout(null);
		
		JLabel lweight = new JLabel("Edge Weight");
		lweight.setBounds(10,10,80,20);
		cpanel.add(lweight);
		weight = new JSpinner(new SpinnerNumberModel(currentEdge.getWeight(), 1,1000,2));
		weight.setBounds(93,10,85,20);
		cpanel.add(weight);
		
		colorLabel = new JLabel("");
		colorLabel.setBounds(80,37,16,16);
		colorLabel.setBorder(new LineBorder(currentEdge.getColor(), 8));
		cpanel.add(colorLabel);
		
		JButton change = new JButton("Change");
		change.setBounds(100,35,80,20);
		cpanel.add(change);
		change.addActionListener(this);
		
		JLabel lcolor = new JLabel("Edge Color");
		lcolor.setBounds(10,35,90,20);
		cpanel.add(lcolor);
		
		getContentPane().add(bpanel, BorderLayout.SOUTH);
		getContentPane().add(cpanel, BorderLayout.CENTER);
		//getRootPane().setDefaultButton(ok);
		
		setLocation(500,100);
		setResizable(false);
		setSize(190,130);
		setVisible(true);
	}
	
	public void actionPerformed(ActionEvent ae)
	{
		String s = ae.getActionCommand();
		
		if(s.equals("Ok"))
		{
			Number h = (Number) weight.getValue();
			currentEdge.setWeight(h.intValue());
			currentEdge.setColor(edgeColor);
			dispose();
		}
		else if(s.equals("Change"))
		{
			JColorChooser cc = new JColorChooser();
			Color c = cc.showDialog(gPanel.gFrame, "Color Chooser", currentEdge.getColor());
			if(c!=null)
			{
				colorLabel.setBorder(new LineBorder(c, 8));
				edgeColor = c;
			}
		}
		else if(s.equals("Cancel"))
		{
			dispose();
		}
		
		gPanel.repaint();
	}
}